package bm.com.framework.common.unit;


import bm.com.framework.common.config.EnumCode;
import bm.com.framework.common.encrypt.MD5Util;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;

import java.lang.management.ManagementFactory;
import java.util.*;
import java.util.stream.Collectors;

public class Tool {

    /**
     * JSON数据，转成Object
     */
    public static <T> T fromJson(String json, Class<T> clazz) {
        return JSONObject.parseObject(json, clazz);
    }

    /**
     * 对象转json
     *
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        return JSONObject.toJSONString( object);
    }

    /**
     * 判断字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isStringEmpty(String str) {
        if (str == "" || "".equals(str) || str == null) {
            return true;
        }
        return false;
    }

    /**
     * 判断对象是否为空
     *
     * @param obj
     * @return
     */
    public static boolean isObjectEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof List) {
            if (((List<?>) obj).isEmpty()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断整型是否为空
     *
     * @param val
     * @return
     */
    public static boolean isIntEmpty(int val) {
        String value = val + "";
        if ("".equals(value)) {
            return true;
        }
        return false;
    }

    public static boolean isLongEmpty(Long val) {
        String value = val + "";
        if ("".equals(value) || Objects.isNull(val)) {
            return true;
    }
        return false;
    }

    /**
     * 判断集合是否为空
     *
     * @param list
     * @return
     */
    public static boolean isListEmpty(List list) {
        if (null == list || list.size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取随机字符串
     *
     * @param type
     * @param size
     * @param isPuu
     * @return
     */
    public static String getRoundString(int type, int size, boolean isPuu) {

        String str = "";
        switch (type) {
            case EnumCode.NUMBER_ONE: {  //数字和字母
                str = EnumCode.RANDOM_MIXTURE;
            }
            break;
            case EnumCode.NUMBER_TWE: { //纯字母
                str = EnumCode.RANDOM_LETTER;
            }
            break;
            case EnumCode.NUMBER_THREE: { //纯数字
                str = EnumCode.RANDOM_NUMBER;
            }
            break;
            default:
                str = EnumCode.RANDOM_MIXTURE;
                break;
        }

        List<String> stringList = Arrays.asList(str.split(""));
        Collections.shuffle(stringList);
        String join = String.join("", stringList).substring(0, size);
        if (isPuu) {
            join = join.toUpperCase();
        }
        return join;
    }

    /**
     * 获取文件类型
     *
     * @param type
     * @return
     */
    public static List<String> getFileType(int type) {

        final List<String> stringList = new ArrayList<>();

        switch (type) {
            case EnumCode.NUMBER_ONE:
                Collections.addAll(stringList, "png", "jpg", "jpeg", "gif", "bmp", "jfif", "pjp", "jpeg", "tga", "psd", "ai", "tif");
                break;
            case EnumCode.NUMBER_TWE:
                Collections.addAll(stringList, "mp4", "mp3", "mov", "avi", "bmp", "wmv", "asf", "3gp", "mkv", "m4a");
                break;
            case EnumCode.NUMBER_THREE:
                Collections.addAll(stringList, "max", "txt", "doc", "ppt", "pdf", "cvs", "xlxs", "dwg", "rar", "zip");
                break;
        }
        return stringList;
    }

    /**
     * 生成token
     * @return
     */
    public static String generateToken(){
        return  MD5Util.toMd5(EnumCode.RANDOM_FIXED_KEY + DateUnit.getNowTimeMilliSecond(),32,false);
    }

    /**
     * 全局唯一id生成器
     * @return
     */
    public static synchronized String iDGenerator(){

    // 算法，毫秒实践戳（13位） + 业务线序列号（4位 ） + 当前进程id号 + 随机数字（ 8位 ）
    int pid = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
    return System.currentTimeMillis() + pid  + "" + Long.parseLong(Tool.getRoundString(3,8,false));

    }

    public static List<Long> splitToLong(String value, CharSequence separator) {
        long[] longs = StrUtil.splitToLong(value, separator);
        return Arrays.stream(longs).boxed().collect(Collectors.toList());
    }

    public static Set<Long> splitToLongSet(String value) {
        return splitToLongSet(value, StrPool.COMMA);
    }

    public static Set<Long> splitToLongSet(String value, CharSequence separator) {
        long[] longs = StrUtil.splitToLong(value, separator);
        return Arrays.stream(longs).boxed().collect(Collectors.toSet());
    }

    public static List<Integer> splitToInteger(String value, CharSequence separator) {
        int[] integers = StrUtil.splitToInt(value, separator);
        return Arrays.stream(integers).boxed().collect(Collectors.toList());
    }

}
